import java.util.Arrays;

//
//顺序栈  入栈和出栈的时间复杂读都是O(1)
public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[10];
    }
    public  void push(int val){
        if(isfull()){
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = val;
    }
    private   boolean isfull(){
        return usedSize == elem.length;
    }
    public  int pop(){
        if(isEmpty()){
            throw  new  EmptyStackException("栈为空");
        }
        int val = elem[usedSize-1];//保存被弹出的值
        usedSize--;//再将栈减减
        return val;
    }

    //获取栈顶元素但是不删除
    public int peek(){
        if(isEmpty()){
            throw  new  EmptyStackException("栈为空");
        }
        return elem[usedSize-1];
    }

    //检查空不空
    private boolean isEmpty(){
        return usedSize ==0;
    }

}
